package com.seu.spring.simple.aop;

import com.seu.spring.service.HelloService;
import com.seu.spring.service.impl.HelloServiceImpl;
import org.junit.Test;

/**
 * 简易AOP测试
 *
 * @author liangfeihu
 * @since 2018/12/27
 */
public class SimpleAOPTest {
    @Test
    public void getProxy() throws Exception {
        // 1.创建一个 MethodInvocation 实现类
        MethodInvocation methodInvocation = () -> System.out.println("log task start");
        HelloService helloService = new HelloServiceImpl();

        // 2.创建一个Advice
        Advice beforeAdvice = new BeforeAdvice(helloService, methodInvocation);

        // 3.为目标对象生成代理
        HelloService helloServiceProxy = (HelloService) SimpleAOP.getProxy(helloService, beforeAdvice);

        // 4.执行业务
        helloServiceProxy.sayHelloWorld();

        System.out.println("--------------------------");

        // 创建After通知
        Advice afterAdvice = new AfterAdvice(helloService, methodInvocation);
        // 获取AOP代理类
        HelloService afterProxy = (HelloService) SimpleAOP.getProxy(helloService, afterAdvice);
        // 执行代理类方法
        afterProxy.sayHelloWorld();
    }
}
